<?
// 
//		Gallery Dataset class v2.6 2012.01.18
//
//		init params:
//			fn				path to gallery directory
//			tn				name of the thumbnails directory
//			large			path to large images directory
//			id_data		DB data id
//
//		req:
//			'key' field in the 'data' table
//
//		+ localization support added

class GALLERY2 {

	var $fn;				// path to gallery directory
	var $tn;				// name of the thumbnails directory

	var $data;				// результат
	
	var $res;				// код ошибки	
	
	var $PREXT;				// possible extensions of preview


	var $err = array (
		0 => 'success',
		1 => 'bad image or image not found',
		2 => 'no file selected',
		255 => 'not initialized'
	);
	
	function GALLERY2() {

		$this->o = array('fn' => '', 'tn' => 'tn', 'large' => '', 'id_data' => D_GALLERY_DATA);
		$this->PREXT = array('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG');
	
		$this->data = array();
		
		$this->res = -255;
	}

	function init( $fn, $tn = 'tn', $id_data = D_GALLERY_DATA ) {
		
		if (is_array($fn))
			$this->o = array_merge($this->o, $fn);
		else {
			$this->o['fn'] = $fn;
			$this->o['tn'] = $tn;
			$this->o['id_data'] = $id_data;
		}
		
		if ($this->o['fn'])
			$this->res = $this->fetch();
		else 
			$this->res = 2;	// проект не выбран

		return ! (bool) $this->res;
	}


	function fetch() {

		$_id_meta		= D_MB_META;			//	meta id value
		$path = $_SERVER['DOCUMENT_ROOT'].$this->o['fn'];

		$_files = array(); 
		if ($dir = glob("$path/*.*", GLOB_NOSORT))
			foreach ($dir as $temp) {
				if ((is_file($temp)) && @filesize($temp) && ($is = @getimagesize($temp)) ) {
					$t = $this->parse_fname($temp);
					$_files["$t[name].$t[ext]"] = array('w' => $is[0], 'h' => $is[1], 'fn' => "$t[name].$t[ext]" );
				}
			}

		$_previews = array();
		if ($this->o['tn'] && ($dir = glob("$path/{$this->o[tn]}/*.*", GLOB_NOSORT)))
			foreach ($dir as $temp) {
				if ((is_file($temp)) && @filesize($temp) && ($is = @getimagesize($temp)) ) {
					$t = $this->parse_fname($temp);
					$_previews["$t[name].$t[ext]"] = array('w' => $is[0], 'h' => $is[1], 'fn' => "$t[name].$t[ext]" );
				}
			}
			
		$_large = array();
		if ($this->o['large'] && ($dir = glob("$path/{$this->o[large]}/*.*", GLOB_NOSORT)))
		foreach ($dir as $temp) {
			if ((is_file($temp)) && @filesize($temp) && ($is = @getimagesize($temp)) ) {
				$t = $this->parse_fname($temp);
				$_large["$t[name].$t[ext]"] = array('w' => $is[0], 'h' => $is[1], 'fn' => "$t[name].$t[ext]" );
			}
		}

		$temp = array();
		if ($this->o['tn']) {
			
			foreach ($_previews as $k => $v)
				if (isset($_files[$k])) {
					$temp[$k] = array_merge($_files[$k], array('tnw' => $v['w'], 'tnh' => $v['h'], 'tn' => "{$this->o[tn]}/$v[fn]") );
					unset($_files[$k]);
				}
				else
					$temp[$k] = array('tnw' => $v['w'], 'tnh' => $v['h'], 'tn' => "{$this->o[tn]}/$v[fn]" );
			
			foreach ($_files as $k => $v)
				$temp[$k] = $v;
				
		} 
			else $temp = $files;


		$temp1 = array();
		if ($this->o['large']) {
			
			foreach ($temp as $k => $v)
				if (isset($temp[$k])) {
					$temp1[$k] = array_merge($temp[$k], array('lw' => $v['w'], 'lh' => $v['h'], 'l' => "{$this->o[large]}/$v[fn]") );
					unset($temp[$k]);
				}
				else
					$temp1[$k] = array('lw' => $v['w'], 'lh' => $v['h'], 'l' => "{$this->o[large]}/$v[fn]" );
			
			foreach ($temp as $k => $v)
				$temp1[$k] = $v;
				
		} 
			else $temp1 = $temp;

		foreach ($temp1 as $k => $v)	// !!! take only records with fn is defined, because it is the db key
			if ($v['fn'])
				$this->data[$v['fn']] = $v;



		$files = "('".implode("','", array_keys($this->data))."')";

		$key = (substr($this->o[fn], -1, 1) == '/') ? array(h($this->o[fn]), h(substr($this->o[fn], 0, -1))) : array(h($this->o[fn]).'/', h($this->o[fn]));
		$key = "'".implode("','", $key)."'";	// 2 versions of path -  with and without trailing slash

		$descr = datafetch_array('db_list', array(
			'table'	=> tb('data'),
			'where'	=> "id_data={$this->o[id_data]} AND assoc in ($key) AND name in $files",
			'order'	=> 'id_index',
			'index'	=> 'name'
		));


		// start lang processing
		$ks = array();	foreach($descr as $v) $ks[] = $v['id']; // $ks = implode(',', $ks);

		if (sizeof($ks)) {

			$t = datafetch_array('loadmeta', $_id_meta, 
				array('parent' => $ks, 'assoc' => context('lang'), 'getclob' => true, 'source' => true));

			$tx = array(); foreach($t as $v) $tx[$v['id_parent']][$v['name']] = $v['value'];
			
			foreach($descr as $k => $v) 	// embed metas into the description array (if any)
				$descr[$k] = array_merge($v, isset($tx[$v['id']]) ? $tx[$v['id']] : array('title' => $v['text'], 'descr' => $v['value']));
		}
		// end lang processing


		$temp = array();	// finally embed descriptions into images' data

		foreach ($descr as $k => $v) {
			$temp[$k] = array_merge((array)$this->data[$k], array('title' => $v['title'], 'descr' => $v['descr']));
			unset($this->data[$k]);
		}

		foreach($this->data as $k => $v) { $temp[$k] = $this->data[$k]; }
		
		$this->data = $temp;


		if (sizeof($this->data))
			return 0;
		else 
			return 1;	// no images
	}
	
	function parse_fname($fname) {
		if (($slash = strrpos($fname, '/')) === false) {
			$fn = $fname;
			$path = '';
		} else {
			$fn = substr($fname, $slash + 1);
			$path = substr($fname, 0, $slash);
		}
		preg_match('/(.+)\.(\w+)$/', $fn, $regs);
		return array('path' => $path, 'name' => $regs[1], 'ext' => $regs[2]);
	}

}
